package com.qidian.mes.dv.service.impl;


import com.qidian.mes.pojo.dv.MachineryType;
import com.qidian.mes.dv.mapper.MachineryTypeMapper;
import com.qidian.mes.dv.service.MachineryTypeService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 迄氺(甄闯)
 * @Company 空白
 * @Time 2023/10/25 20:06
 * @Description
 */
@Service
@Transactional
public class MachineryTypeServiceImpl implements MachineryTypeService {

    @Autowired
    private MachineryTypeMapper machineryTypeMapper;
    @Override
    public MESResult selectById(Long machineryTypeId) {
        MachineryType machineryType =null;
        if((machineryType=machineryTypeMapper.selectById(machineryTypeId))!=null){
            return MESResult.ok(machineryType);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult save(MachineryType machineryType) {
        //效验设备类型是否重复
        if (machineryType.getMachineryTypeName() != null && machineryTypeMapper.selectByName(machineryType.getMachineryTypeName())!=null){
            return MESResult.duplicate("设备类型名称重复");
        }
        return MESResult.updateIsOk(machineryTypeMapper.save(machineryType));
    }

    @Override
    public MESResult update(MachineryType machineryType) {
        //校验是否重复
        MachineryType machineryType1 = machineryTypeMapper.selectByName(machineryType.getMachineryTypeName());
        if (machineryType1 != null && !machineryType1.getMachineryTypeId().equals(machineryType.getMachineryTypeId())){
            return MESResult.duplicate("设备类型名称重复");
        }
        return MESResult.updateIsOk(machineryTypeMapper.update(machineryType));
    }

    @Override
    public MESResult deleteById(Long machineryTypeId) {
        return MESResult.updateIsOk(machineryTypeMapper.deleteByMachineryTypeId(machineryTypeId));
    }
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public MESResult selectAll(String machineryTypeName,String enableFlag) {

        MachineryTypeService machineryTypeService =(MachineryTypeService) AopContext.currentProxy();
        List<MachineryType> machineryTypes = machineryTypeService.selectByParentTypeId(0L,machineryTypeName,enableFlag);
        System.out.println("srers=>>>>>>>>>>>>"+machineryTypes);
        return MESResult.ok(machineryTypes);
    }
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<MachineryType> selectByParentTypeId(Long parentTypeId,String machineryTypeName,String enableFlag) {
        //查到全部的数据了 接下来就是递归组装
        List<MachineryType> machineryTypes = machineryTypeMapper.list(machineryTypeName,enableFlag);
        //先用 传进来的参数 过滤出第一层 //
        List<MachineryType> returnList = new ArrayList<>();
       if (StringUtils.isEmpty(machineryTypeName) && StringUtils.isEmpty(enableFlag)){
           returnList = machineryTypes.stream().filter(m->m.getParentTypeId().equals(parentTypeId)).collect(Collectors.toList());
       }else if (!StringUtils.isEmpty(machineryTypeName)&&StringUtils.isEmpty(enableFlag)){
           returnList =machineryTypes.stream().filter(m->m.getMachineryTypeName().contains(machineryTypeName)).collect(Collectors.toList());
       }else if (StringUtils.isEmpty(machineryTypeName)&&!StringUtils.isEmpty(enableFlag)){
           returnList=machineryTypes.stream().filter(m->m.getEnableFlag().equals(enableFlag)).collect(Collectors.toList());
       }else {
           returnList = machineryTypes.stream().filter(m->m.getEnableFlag().equals(enableFlag)&&m.getMachineryTypeName().contains(machineryTypeName)).collect(Collectors.toList());
       }
        //写一个递归方法
        returnList.forEach(r->{
            buildMenu(r);
        });
        return returnList;
    }


    private void buildMenu(MachineryType parent){
        List<MachineryType> children = machineryTypeMapper.selectByParentTypeId(parent.getMachineryTypeId() );
        if(!CollectionUtils.isEmpty(children)){
//            parent.setHasChildren(true);
            parent.setChildren(children);
            children.forEach(c -> {
                buildMenu(c);
            });
        }
    }
}
